import { IpcAPI, WebSocketConnectionEvent, WebSocketDisconnectionEvent, WebSocketMessageEvent, ProgressEvent, CompleteEvent, ErrorEvent } from '../types/ipc';

declare global {
  interface Window {
    electron?: {
      // Electron 工具包提供的 API
    };
    api?: any; // 你的预加载脚本暴露的 API
  }
}

class ElectronAdapter implements IpcAPI {
  private get api(): any {
    if (!window.api) {
      throw new Error('Electron API is not available');
    }
    return window.api;
  }

  get log() {
    return this.api.log;
  }

  downloadFile(payload: any): void {
    this.api.downloadFile(payload);
  }

  saveBase64Image(params: { base64Data: string; directory: string; fileName: string; imagesPath: string }): Promise<any> {
    return this.api.saveBase64Image(params);
  }

  onDownloadProgress(callback: (progress: any) => void): () => void {
    this.api.onDownloadProgress(callback);
    return () => this.api.removeListener('download-progress', callback);
  }

  onDownloadComplete(callback: (result: any) => void): () => void {
    this.api.onDownloadComplete(callback);
    return () => this.api.removeListener('download-complete', callback);
  }

  onDownloadError(callback: (error: any) => void): () => void {
    this.api.onDownloadError(callback);
    return () => this.api.removeListener('download-error', callback);
  }

  readMetadata(filePath: string): Promise<any> {
    return this.api.readMetadata(filePath);
  }

  readFolderMeta(): Promise<any> {
    return this.api.readFolderMeta();
  }

  getGlobalConfig(): Promise<any> {
    return this.api.getGlobalConfig();
  }

  saveConfig(config: any): Promise<any> {
    return this.api.saveConfig(config);
  }

  openFileDialog(): Promise<any> {
    return this.api.openFileDialog();
  }

  selectFileFolder(): Promise<any> {
    return this.api.selectFileFolder();
  }

  openFileFolder(folderPath: string): Promise<any> {
    return this.api.openFileFolder(folderPath);
  }

  scanDirectories(targetPath: string): Promise<any> {
    return this.api.scanDirectories(targetPath);
  }

  scanImages(basePath: string, folderName: string): Promise<any> {
    return this.api.scanImages(basePath, folderName);
  }

  scanLoras(loraPath: string): Promise<any> {
    return this.api.scanLoras(loraPath);
  }

  getLoraConfig(loraPath: string): Promise<any> {
    return this.api.getLoraConfig(loraPath);
  }

  saveLoraConfig(loraPath: string, data: any): Promise<any> {
    return this.api.saveLoraConfig(loraPath, data);
  }

  saveLoraImage(imagePath: string, loraPath: string): Promise<any> {
    return this.api.saveLoraImage(imagePath, loraPath);
  }

  cacheImage(cachePath:string,array:ArrayBuffer):Promise<any> {
    return this.api.cacheImage(cachePath, array);
  }

  getLoraMetadata(loraPath: string): Promise<any> {
    return this.api.getLoraMetadata(loraPath);
  }

  getCivitaiMetadata(sha256: string|undefined,loraPath: string,replaceCover: boolean|undefined): Promise<any> {
    return this.api.getCivitaiMetadata(sha256,loraPath,replaceCover);
  }

  localSet(key: string, value: any): Promise<any> {
    return this.api.localSet(key, value);
  }

  localGet(key: string): Promise<any> {
    return this.api.localGet(key);
  }

  localRemove(key: string): Promise<any> {
    return this.api.localRemove(key);
  }

  localClear(): Promise<any> {
    return this.api.localClear();
  }

  sessionSet(key: string, value: any): Promise<any> {
    return this.api.sessionSet(key, value);
  }

  sessionGet(key: string): Promise<any> {
    return this.api.sessionGet(key);
  }

  sessionRemove(key: string): Promise<any> {
    return this.api.sessionRemove(key);
  }

  sessionClear(): Promise<any> {
    return this.api.sessionClear();
  }

  sendWebsocketData(data:any,type:string): Promise<any> {
    return this.api.sendWebsocketData(data,type);
  }

  onWebSocketConnection(callback: (event: WebSocketConnectionEvent) => void): () => void {
    return this.api.onWebSocketConnection(callback);
  }

  onWebSocketDisconnection(callback: (event: WebSocketDisconnectionEvent) => void): () => void {
    return this.api.onWebSocketDisconnection(callback);
  }

  onWebSocketMessage(callback: (event: WebSocketMessageEvent) => void): () => void {
    return this.api.onWebSocketMessage(callback);
  }

  broadcastMessage(message: string): Promise<any> {
    return this.api.broadcastMessage(message);
  }

  sendToClient(clientId: string, message: string,type: string): Promise<any> {
    return this.api.sendToClient(clientId, message,type);
  }

  getSocketClients(): Promise<any> {
    return this.api.getSocketClients();
  }

  onCsvImportProgress(callback: (progress: any) => void): void {
    this.api.onCsvImportProgress(callback);
  }

  onImportProgress(callback: (progress: any) => void): ()=> void {
    return this.api.onImportProgress(callback);
  }
}

export default ElectronAdapter;
